//for循环
var num: number = 5;
var i: number;
var factorial = 1;

for (i = num; i >= 1; i--) {
    factorial *= i;
}
console.log(factorial)

//for in 循环
//语法  for (var val in list) { 
//语句 
//}  val为string或any类型
var j: any;
var n1: any = "a b c"

for (j in n1) {
    console.log(n1[j])
}
//for of
let someArray = [1, "string", false];

for (let entry of someArray) {
    console.log(entry); // 1, "string", false
}
//forEach
let list = [4, 5, 6];
list.forEach((val, idx, array) => {
    // val: 当前值
    // idx：当前index
    // array: Array
});

//every
let list1 = [4, 5, 6];
list.every((val, idx, array) => {
    // val: 当前值
    // idx：当前index
    // array: Array
    return true; // Continues
    // Return false will quit the iteration
});

//while
var num: number = 5;
var factorial: number = 1;

while (num >= 1) {
    factorial = factorial * num;
    num--;
}
console.log("5 的阶乘为：" + factorial);
//do while
var l: number = 10;
do {
    console.log(l);
    l--;
} while (l >= 0);

//break
var i: number = 1
while (i <= 10) {
    if (i % 5 == 0) {
        console.log("在 1~10 之间第一个被 5 整除的数为 : " + i)
        break     // 找到一个后退出循环
    }
    i++
}  // 输出 5 然后程序执行结束

//continue
var num: number = 0
var count: number = 0;

for (num = 0; num <= 20; num++) {
    if (num % 2 == 0) {
        continue
    }
    count++
}
console.log("0 ~20 之间的奇数个数为: " + count)    //输出10个偶数

//构造函数
var myFunction = new Function("a", "b", "return a * b");
var x1= myFunction(4, 3);
console.log(x1);

//递归函数
function factorial2(number) {
    if (number <= 0) {         // 停止执行
        return 1;
    } else {
        return (number * factorial2(number - 1));     // 调用自身
    }
};
console.log(factorial2(6));      // 输出 720

//箭头函数
var foo = (x:number)=> {    
    x = 10 + x 
    console.log(x)  
} 
foo(100)